package com.zouxian.processor;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.sun.jna.platform.win32.OaIdl.CURRENCY;
import com.zouxian.phantom.TestPhantomJsDriver;
import com.zouxian.pipeline.MyFilePipeline;
import com.zouxian.pojo.News;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

public class MyProcessor8_xinlang implements PageProcessor {

	private Site site = Site.me().setRetryTimes(3).setSleepTime(0).setTimeOut(3000);
	private String keyword="国美";
	// 用来存储cookie信息
	private Set<Cookie> cookies;
	//设置当前页面url
	private static String currentUrl;

	@Override
	public void process(Page page) {
		// 获取用户的id
		News news=new News();
		page.addTargetRequests(page.getHtml().xpath("//*[@id='result']/div/div/h2/a/@href").all());

		//获取url
		String Url=page.getUrl().get();
		news.setUrl(Url);
		//获取标题
		//String title=page.getHtml().xpath("//*[@id=\"article\"]/div[2]/div/div[1]/header/div[3]/div[1]/div/h1/font").get();
		String title=page.getHtml().$(".main-title","text").toString();
		
		if(title==null){
			//System.out.println("该链接没有标题，不符合要求！");
			page.setSkip(true);
			
		}
		news.setTitle(title);
		//获取关键字
		//String articalKeyWords=page.getHtml().$("div.submeta__section-labels ul li a","allText").all().toString();
		//news.setArticalKeyWords(articalKeyWords);
		//获取日期
		String GetTime=page.getHtml().$(".date ","allText").all().toString();
		news.setGetTime(GetTime);
		System.out.println(GetTime);
		//获取正文
		String Txt=page.getHtml().$(".article", "allText").all().toString();
		news.setTxt(Txt);
		//获取摘要 tonal__standfirst u-cf
		//String Summary=page.getHtml().$("div.content__standfirst p", "text").all().toString();
		//news.setSummary(Summary);
		//获取文章类型
		String TxtType=page.getHtml().$(".channel-path","allText").all().toString();
		news.setTxtType(TxtType);
		//获取作者
		//String author=page.getHtml().$("span[itemprop=name]", "text").all().toString();
		//news.setAuthor(author);
		page.putField("title", news.getTitle());
		page.putField("TitleCh", news.getTitleCh());
		page.putField("Title_Score", news.getTitle_Score());
		page.putField("articalKeyWords", news.getArticalKeyWords());
		page.putField("Txt", news.getTxt());
		page.putField("TxtCh", news.getTxtCh());
		page.putField("Txt_Score", news.getTxt_Score());
		page.putField("Url", news.getUrl());
		page.putField("Summary", news.getSummary());
		page.putField("SummaryCh", news.getSummaryCh());
		page.putField("TxtType", news.getTxtType());
		page.putField("GetTime", news.getGetTime());
		page.putField("opicType", news.getOpicType());
		page.putField("Source", news.getSource());
		page.putField("isdown", news.getIsdown());
		page.putField("relateNewsNumber", news.getRelateNewsNumber());
		page.putField("KeyWords", news.getKeyWords());
		page.putField("schoolName", news.getSchoolName());
		page.putField("country", news.getCountry());
		page.putField("Polar", news.getPolar());
		page.putField("author", news.getAuthor());
		page.putField("language", news.getLanguage());
		page.putField("htmlCode", news.getHtmlCode());
		page.putField("semanticValue", news.getSemanticValue());
		page.putField("mediaIntroduction", news.getMediaIntroduction());
		page.putField("zuanfaNum", news.getZuanfaNum());
		page.putField("pinlunNum", news.getPinlunNum());
		page.putField("shouchangNum", news.getSchoolName());
		page.putField("txtSeg", news.getTxtSeg());
		

	}

	// 使用 selenium 来模拟用户的登录获取cookie信息
	public void login() {
		//设置PhantomJS配置路径
		//System.setProperty("phantomjs.binary.path", "D:\\developTools\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");		
		//System.setProperty("phantomjs.binary.path", "D:\\crawl.js");
		DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();
		//WebDriver driver = new PhantomJSDriver();
		WebDriver driver=TestPhantomJsDriver.getPhantomJSDriver();
		
		driver.get(
				"https://tieba.baidu.com/index.html");
		
		
		//driver.findElement(By.name("q")).clear();

		// 在******中填你的用户名
		//driver.findElement(By.name("q")).sendKeys(keyword);

		

		// 模拟点击登录按钮
		driver.findElement(By.className("search_btn j_search_post")).click();
		
		cookies = driver.manage().getCookies();
		//获取当前页面url
		currentUrl = driver.getCurrentUrl();
		driver.close();
	}

	@Override
	public Site getSite() {

		// 将获取到的cookie信息添加到webmagic中
		for (Cookie cookie : cookies) {
			site.addCookie(cookie.getName().toString(), cookie.getValue().toString());
		}

		return site.addHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1");
	}

	public static void main(String[] args) {
		MyProcessor8_xinlang miai = new MyProcessor8_xinlang();

		// 调用selenium，进行模拟登录
		miai.login();
		Spider.create(miai).addUrl(currentUrl).addPipeline(new ConsolePipeline()).thread(5).run();
	}
}
